<?php

require_once 'Ext/User/Edit/Password/Abstract.php';

class Ext_User_Edit_Password_Adapter_Db extends Ext_User_Edit_Password_Abstract
{
    const RECORD_NOT_FOUND        = 'RecordNotFound';
    protected $_messageTemplates = array(
        self::RECORD_NOT_FOUND       => "Record not found.",
    );
    
    public function edit($password)
    {
        $result = array(
            'code'        => Ext_User_Edit_Password_Result::FAILURE,
            'messages'    => array(),
        );
        
	$userAccountBase = App_Model_DbTable_User_Account_Base::getInstance()->getRowByUserId($this->_userId);
        if(!$userAccountBase)
        {
            $result['messages'][] = $this->_messager->createMessage(self::RECORD_NOT_FOUND);
            return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
        }
        
        $row = array(
            'password' => md5($password),
        );
        App_Model_DbTable_User_Account_Base::getInstance()->addOld($row, $this->_userId);

        $result['code'] = Ext_User_Edit_Email_Result::SUCCESS;
        return new Ext_User_Edit_Password_Result($result['code'], $result['messages']);
    }
}
